<template>
	<zh-form ref="userForm" :span="24" :model="state.form" :rules="state.rules">
		<zh-form-input prop="userName" label="账号" v-model="state.form.userName" :disabled="true"></zh-form-input>
		<zh-form-input prop="fullName" label="账号名称" v-model="state.form.fullName" :disabled="true"></zh-form-input>
		<zh-form-input prop="userPassword" label="原密码" v-model="state.form.userPassword" type="password" placeholder="请输入密码" show-password />
		<zh-form-input
			prop="newUserPassword1"
			label="新密码"
			v-model="state.form.newUserPassword1"
			type="password"
			placeholder="请输入新密码"
			show-password
		/>
		<zh-form-input
			prop="newUserPassword"
			label="再次输入新密码"
			v-model="state.form.newUserPassword"
			type="password"
			placeholder="请再次输入新密码"
			show-password
		/>
	</zh-form>
</template>

<script lang="ts" setup name="PersonPasswordForm">
import { ElMessage } from 'element-plus';

import { checkPasswordRule } from '@/utils/checkPassword';
const validatePassword = (rule: any, value: string, callback: any) => {
	if (!value) {
		callback(new Error('请输入新密码'));
	} else {
		let name: string = '';
		if (state.form.userName === '') {
			name = '空';
		} else {
			name = state.form.userName || '空';
		}
		const result: string = checkPasswordRule(value, name);
		if (result === '校验通过') {
			callback();
		} else {
			callback(new Error(result));
		}
	}
};
//------------------接收传入参数(组件)-------------------
const emit = defineEmits(['update:modelValue']);
const props = defineProps({
	userInfo: {
		type: Object,
		default: () => {},
		required: false,
	},
	modelValue: Boolean,
});

//------------------定义子组件---------------------------
const userForm = ref<any>({});

//------------------内部变量定义-------------------------
const state = reactive({
	rules: reactive({
		newUserPassword1: [{ required: true,validator: validatePassword, trigger: 'blur' }],
		newUserPassword: [{ required: true,validator: validatePassword, trigger: 'blur' }],
		userPassword: [
			{
				required: true,
				message: '请输入原密码',
				trugger: 'blur',
			},
		],
	}),
	userTypeDisabled: true,
	dictData: {} as any,
	form: props.userInfo,
});

//------------------定义调用方法-------------------------

async function savePassword() {
	//校验表单
	let checkFlag = await userForm.value.checkForm();
	if (checkFlag) {
		if(state.form.newUserPassword1!==state.form.newUserPassword){
			ElMessage.error('两次输入的密码不同，请核对');
			return;
		}
		if(state.form.userPassword===state.form.newUserPassword){
			ElMessage.error('旧密码和新密码相同，不能修改');
			return;
		}
		const userEditPasswordVO = state.form;
		let ret = null as any;
		if (userEditPasswordVO.userId) {
			ret = await $api.common.send.postCrypto('user0005', userEditPasswordVO);
			if (ret.code === 0) {
				emit('update:modelValue', false);
				ElMessage.success('修改成功！');
			}
		}
		return ret;
	}
	return null;
}

//------------------初始化------------------------------

// 数据字典初始化
const params = {} as any;
params.dictFields = 'userType,sex,cardType';
$api.common.getBatchDict.post(params).then((res) => {
	state.dictData = res.result;
});
watch(
	() => props.userInfo,
	(val) => {
		state.form=val
	}
);
//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({ form: state.form, savePassword });
</script>
